package com.jianyun.jyzs.dao;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.jianyun.jyzs.bean.CrmFunnelList;
import com.jianyun.jyzs.bean.EnNatureResponse;
import com.jianyun.jyzs.db.DatabaseHelper;


import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by mlli on 2017/8/23.
 */

public class CrmFunnelDao {
    private Context context;
    private Dao<CrmFunnelList.FunnelBean,Integer> funnelDao;
    private DatabaseHelper helper;

    public CrmFunnelDao(Context context) {
        this.context = context;
        try {
            helper = DatabaseHelper.getHelper(context);
            funnelDao = helper.getDao(CrmFunnelList.FunnelBean.class);
        }catch (Exception e){

        }
    }
    public void insertFunnel(CrmFunnelList.FunnelBean funnelBean, String enterpriseCode){
        try {
            funnelBean.setEnterpriseCode(enterpriseCode);
            funnelDao.createOrUpdate(funnelBean);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public List<CrmFunnelList.FunnelBean> getFunnelList(String enterpriseCode){

        try {
            QueryBuilder<CrmFunnelList.FunnelBean, Integer> builder = funnelDao.queryBuilder();
            Where<CrmFunnelList.FunnelBean, Integer> where = builder.where().eq("enterpriseCode", enterpriseCode);
            return where.query();
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<String[]> getStringList(String enterpriseCode) {
        try {
            QueryBuilder<CrmFunnelList.FunnelBean, Integer> builder = funnelDao.queryBuilder();
            builder.orderBy("id", true);
            builder.where().eq("enterpriseCode", enterpriseCode);
            List<CrmFunnelList.FunnelBean> list = builder.query();
            List<String[]> arrList = new ArrayList<String[]>();
            String[] str = null;
            if(list != null){
                for(CrmFunnelList.FunnelBean f: list){
                    str = new String[3];
                    str[0] = "0";
                    str[1] = f.getName();
                    str[2] = f.getId();
                    arrList.add(str);
                }
            }
            return arrList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ArrayList<String[]>();
    }

    public List<String> getStringList2(String enterpriseCode) {
        List<String> strings = new ArrayList<>();
        try {
            QueryBuilder<CrmFunnelList.FunnelBean, Integer> builder = funnelDao.queryBuilder();
            builder.where().eq("enterpriseCode", enterpriseCode);
            List<CrmFunnelList.FunnelBean> list = builder.query();
            if(list != null){
                for(CrmFunnelList.FunnelBean f: list){
                    strings.add(f.getName());
                }
            }
            return strings;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strings;

    }

    public void clear(String enterpriseCode) {
        DeleteBuilder<CrmFunnelList.FunnelBean, Integer> deleteBuilder = funnelDao.deleteBuilder();
        try {
            deleteBuilder.where().eq("enterpriseCode",enterpriseCode);
            deleteBuilder.delete();
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }
    }

    public void delelteAll(){
        DeleteBuilder<CrmFunnelList.FunnelBean, Integer> deleteBuilder = funnelDao.deleteBuilder();
        try {
            deleteBuilder.delete();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
